<%= page_title "Commands" %>

<%= search_form do %>
  <%= render 'shared/search_query' %>
  <%= search_select :project_id, [['Global', 'global']] + Project.order(:name).pluck(:name, :id), live: true, size: 3 %>
<% end %>

<section class="clearfix">
  <table class="table table-hover table-condensed">
    <thead>
      <tr>
        <th>Command</th>
        <th>Project</th>
        <th>Actions</th>
      </tr>
    </thead>

    <tbody>
      <% usage_ids = Command.usage_ids %>
      <% @commands.each do |command| %>
        <tr>
          <td>
            <pre class="pre-command pre-scrollable"><%= command.command %></pre>
          </td>
          <td><%= command.project&.name ? command.project.name : "Global" %></td>
          <td>
            <%= link_to "Edit", command %>
            |
            <% used = usage_ids.count(command.id) %>
            <% if used > 0 %>
              <%= content_tag :span, "Used by #{used}", title: "Click Edit to see usage details" %>
            <% else %>
              <%= link_to_delete(command, remove_container: 'tr') %>
            <% end %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>

  <div class="admin-actions">
    <div class="pull-right">
      <%= link_to "New", new_command_path, class: "btn btn-default" %>
    </div>

    <%= paginate @pagy %>
  </div>
</section>
